#encoding:utf8
from rpc import ChannelSync, Proxy
import transfer_pb2

class StorageConnector(object):
    def __init__(self):
        self.channel = ChannelSync()
        self.proxy = Proxy(transfer_pb2.Transfer_Stub(self.channel))
        
    def Connect(self, host, port):
        self.channel.Connect((host, port))
        
    def Close(self):
        self.channel.Close()
        
    def Write(self, blockId, data):
        p = transfer_pb2.WriteParameters()
        p.blockId = blockId
        p.data = data
        self.proxy.Transfer.Write(p)
        
    def Read(self, blockId, offset, length):
        p = transfer_pb2.ReadParameters()
        p.blockId = blockId
        p.offset = offset
        p.length = length
        return self.proxy.Transfer.Read(p)
    
    def GetBlockSize(self, blockId):
        p = transfer_pb2.GetBlockSizeParameters()
        p.blockId = blockId
        return self.proxy.Transfer.GetBlockSize(p)